﻿using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using BingleMaps.Models;
using DropNet.Models;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Shell;
using Serialization;

namespace BingleMaps.ViewModels
{
	public class SettingsViewModel : ViewModelBase
	{
		public bool IsFirstTime = true;

		[DoNotSerialize]
		private const string PreventScreenLockPropertyName = "PreventScreenLock";
		private bool _preventScreenLock;
		public bool PreventScreenLock
		{
			get { return _preventScreenLock; }
			set
			{
				if (value == _preventScreenLock) return;
				_preventScreenLock = value;
				RaisePropertyChanged(PreventScreenLockPropertyName);
				PhoneApplicationService.Current.UserIdleDetectionMode = value ? IdleDetectionMode.Disabled : IdleDetectionMode.Enabled;
			}
		}

		public enum UnitKey { Auto, Metric, Imperial }
		[DoNotSerialize]
		private const string UnitStringMetric = "km", UnitStringImperial = "mi";
		[DoNotSerialize]
		private static readonly Dictionary<UnitKey, string> _availableDistanceUnits = new Dictionary<UnitKey, string>
		{
			{UnitKey.Auto , RegionInfo.CurrentRegion.IsMetric ? UnitStringMetric : UnitStringImperial},
			{UnitKey.Metric, UnitStringMetric},
			{UnitKey.Imperial, UnitStringImperial}
		};
		[DoNotSerialize]
		public static Dictionary<UnitKey, string> AvailableDistanceUnits { get { return _availableDistanceUnits; } }
		[DoNotSerialize]
		public static readonly KeyValuePair<UnitKey, string> DefaultDistanceUnit = AvailableDistanceUnits.First();

		[DoNotSerialize]
		private const string DistanceUnitPropertyName = "DistanceUnit";
		private UnitKey _distanceUnit;
		public UnitKey DistanceUnit
		{
			get { return _distanceUnit; }
			set
			{
				if (value == _distanceUnit) return;
				_distanceUnit = value;
				RaisePropertyChanged(DistanceUnitPropertyName);
			}
		}

		[DoNotSerialize]
		private static readonly Dictionary<string, string> _availablePushpinIcons = new Dictionary<string, string>
		{
			{"general","/Resources/Icons/Pushpins/PushpinLocation.png"},
			{"recreation","/Resources/Icons/Pushpins/PushpinBicycle.png"},
			{"vehicle","/Resources/Icons/Pushpins/PushpinCar.png"},
			{"drink","/Resources/Icons/Pushpins/PushpinDrink.png"},
			{"fuel","/Resources/Icons/Pushpins/PushpinFuel.png"},
			{"property","/Resources/Icons/Pushpins/PushpinHouse.png"},
			{"restaurant","/Resources/Icons/Pushpins/PushpinRestaurant.png"},
			{"shop","/Resources/Icons/Pushpins/PushpinShop.png"}
		};
		[DoNotSerialize]
		public static Dictionary<string, string> AvailablePushpinIcons { get { return _availablePushpinIcons; } }
		[DoNotSerialize]
		public static readonly KeyValuePair<string, string> DefaultPushpinIcon = AvailablePushpinIcons.First();

		[DoNotSerialize]
		private static readonly Dictionary<string, string> _availablePushpinColors = new Dictionary<string, string>
		{
			{"blue","#1BA1E2"},
			{"magenta","#FF0097"},
			{"purple","#A200FF"},
			{"teal","#00ABA9"},
			{"lime","#8CBF26"},
			{"brown","#A05000"},
			{"pink","#E671B8"},
			{"orange","#F09609"},
			{"red","#E51400"},
			{"green","#339933"}
		};
		[DoNotSerialize]
		public static Dictionary<string, string> AvailablePushpinColors { get { return _availablePushpinColors; } }
		[DoNotSerialize]
		public static readonly KeyValuePair<string, string> DefaultPushpinColor = AvailablePushpinColors.First();

		[DoNotSerialize]
		private const string MapSourceGoogle = "Google Maps", MapSourceBing = "Bing Maps", MapViewStreet = "Street", MapViewSatellite = "Satellite";
		[DoNotSerialize]
		private static readonly string[] _availableMapSources = { MapSourceGoogle, MapSourceBing };

		[DoNotSerialize]
		public static string[] AvailableMapSources { get { return _availableMapSources; } }
		[DoNotSerialize]
		public static readonly string DefaultMapSource = AvailableMapSources.First();

		[DoNotSerialize]
		private static readonly Dictionary<string, string> _availableMapViews = new Dictionary<string, string>
		{
			{MapViewStreet, "/Resources/Images/MapViewStreet.png"},
			{MapViewSatellite, "/Resources/Images/MapViewSatellite.png"}
		};
		[DoNotSerialize]
		public static Dictionary<string, string> AvailableMapViews { get { return _availableMapViews; } }
		[DoNotSerialize]
		public static readonly KeyValuePair<string, string> DefaultMapView = _availableMapViews.First();

		private string _mapView = DefaultMapView.Key;
		public string MapView
		{
			get { return _mapView; }
			set
			{
				if (value == _mapView) return;
				_mapView = value;
				RaisePropertyChanged("MapView");
				UpdateMapTileSource();
			}
		}

		private string _mapSource = DefaultMapSource;
		public string MapSource
		{
			get { return _mapSource; }
			set
			{
				if (value == _mapSource) return;
				_mapSource = value;
				RaisePropertyChanged("MapSource");
				UpdateMapTileSource();
			}
		}

		private void UpdateMapTileSource()
		{
			MapTileSource =
				MapSource == MapSourceGoogle ?
				new GoogleMapSource { MapType = MapView == MapViewStreet ? GoogleMapsType.Street : GoogleMapsType.Hybrid } :
				MapView == MapViewStreet ? new BingMapsRoad() as TileSource : new BingMapsAerial() as TileSource;
		}

		[DoNotSerialize]
		private TileSource _mapTileSource = new GoogleMapSource { MapType = GoogleMapsType.Street };
		[DoNotSerialize]
		public TileSource MapTileSource
		{
			get { return _mapTileSource; }
			set
			{
				if (value == _mapTileSource) return;
				_mapTileSource = value;
				RaisePropertyChanged("MapTileSource");
			}
		}

		private UserLogin _dropNetUserLogin;
		public UserLogin DropNetUserLogin
		{
			get { return _dropNetUserLogin; }
			set
			{
				if (value == _dropNetUserLogin) return;
				_dropNetUserLogin = value;
				RaisePropertyChanged("DropNetUserLogin");
			}
		}

		private string _userLoginName = null;
		public string UserLoginName
		{
			get { return _userLoginName; }
			set
			{
				if (value == _userLoginName) return;
				_userLoginName = value;
				RaisePropertyChanged("UserLoginName");
			}
		}

		private bool _showZoomBar = true;
		public bool ShowZoomBar
		{
			get { return _showZoomBar; }
			set
			{
				if (value == _showZoomBar) return;
				_showZoomBar = value;
				RaisePropertyChanged("ShowZoomBar");
			}
		}

		private bool _sync;
		public bool Sync
		{
			get { return _sync; }
			set
			{
				if (value == _sync) return;
				_sync = value;
				RaisePropertyChanged("Sync");
			}
		}

		private bool _useMapAnimation = true;
		public bool UseMapAnimation
		{
			get { return _useMapAnimation; }
			set
			{
				if (value == _useMapAnimation) return;
				_useMapAnimation = value;
				RaisePropertyChanged("UseMapAnimation");
			}
		}

		private bool _enableLocationService;
		public bool EnableLocationService
		{
			get { return _enableLocationService; }
			set
			{
				if (value == _enableLocationService) return;
				_enableLocationService = value;
				RaisePropertyChanged("EnableLocationService");
			}
		}

		private bool _showMapView = true;
		public bool ShowMapView
		{
			get { return _showMapView; }
			set
			{
				if (value == _showMapView) return;
				_showMapView = value;
				RaisePropertyChanged("ShowMapView");
			}
		}

		const string IsWalkingPropertyName = "IsWalking";
		bool _isWalking;
		public bool IsWalking
		{
			get { return _isWalking; }
			set
			{
				if (value == _isWalking) return;
				_isWalking = value;
				RaisePropertyChanged(IsWalkingPropertyName);
			}
		}
	}
}